/**
 * Created by g705353 on 2/7/14.
 */


import groovy.sql.Sql;
import java.sql.ResultSetMetaData

File file = new File("123.txt");
String strSql = "select * from company_dim";
// oracle.jdbc.OracleDriver  jdbc:oracle:thin:@192.168.1.23:1521:mydb
//Sql sql = Sql.newInstance("jdbc:h2:tcp://localhost/~/db;MVCC=TRUE", "username", "password", "org.h2.Driver");
Sql sql = Sql.newInstance("jdbc:oracle:thin:@bluefish:5204:CDRD11G", "cdr_owner29100", "dev29100", "oracle.jdbc.driver.OracleDriver");
def colNames = []
sql.query(strSql) {resultSet ->
    ResultSetMetaData metaData = resultSet.metaData
    println "Table name is " + metaData.getTableName(1)
    for (i in 0..<metaData.columnCount) {
        println "${i + 1} - " + metaData.getColumnLabel(i + 1) + ":" + metaData.getColumnTypeName(i + 1)
        colNames << metaData.getColumnLabel(i + 1)
    }
}

file.withWriter("UTF-8") {out ->
    for (i in 0..<colNames.size) {
        out.print colNames[i] + ","
    }
    out.println()
    sql.query(strSql) {resultSet ->
        while (resultSet.next()) {
            for (i in 0..<colNames.size) {
                out.print resultSet.getString(colNames[i])
                out.print ","
            }
            out.println()
        }
    }
}